<template>
  <div>
    <van-nav-bar title="黑马头条-移动版" :placeholder='true' :fixed="true" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        name="mobile"
        label="手机号"
        required
        placeholder="手机号"
        :rules="[{ required: true,pattern:/^1[3-9]\d{9}$/, message: '请填写手机号' }]"
      />
      <van-field
        v-model="form.code"
        type="password"
        name="code"
        required
        label="验证码"
        placeholder="密码"
        :rules="[{ required: true,pattern:/^\d{6}$/, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
        round block type="info"
        native-type="submit"
        :disabled='islogin'
         :loading="islogin"
         loading-text="登录中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { Notify } from 'vant'
import { setToken } from '@/utils/token'
export default {
  data () {
    return {
      form: {
        mobile: 13888888888,
        code: 246810
      },
      islogin: false

    }
  },
  methods: {
    async onSubmit (values) {
      this.islogin = true
      try {
        const res = await loginAPI({ ...this.form })
        Notify({ type: 'success', message: '登录成功' })
        this.islogin = false
        setToken(res.data.data.token)
        this.$router.push('/layout/home')
      } catch (err) {
        console.log(err)
        Notify({ type: 'danger', message: '登陆失败' })
        this.islogin = false
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
